package net.versiforma.libraryreminder;

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Flags.Flag;

import net.versiforma.libraryreminder.emails.EmailMessage;


import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;


public class Mailer {

	private static String SMTP_HOST;
	private static String SMTP_HOST_PORT;
	private static String SMTP_AUTH_USER;
	private static String SMTP_AUTH_PWD;
	private static String FROM_ADDRESS;
	private static String SMTP_PROTOCOL;
	
	private Properties properties;
	
	
    public Mailer(String smtpHost, String smtpPort, String protocol, String smtpUsername, String smtpPassword, String fromAddress) {
    	SMTP_HOST = smtpHost;
    	SMTP_HOST_PORT = smtpPort;
    	SMTP_PROTOCOL = protocol;
    	SMTP_AUTH_USER = smtpUsername;
    	SMTP_AUTH_PWD = smtpPassword;
    	FROM_ADDRESS = fromAddress;
	}
    
    public Mailer(Properties properties) {
    	this.properties = properties;
    	SMTP_HOST = properties.getProperty("smtp_hostname");
    	SMTP_HOST_PORT = properties.getProperty("smtp_port");
    	SMTP_PROTOCOL = properties.getProperty("smtp_protocol");
    	SMTP_AUTH_USER = properties.getProperty("smtp_username");
    	SMTP_AUTH_PWD = properties.getProperty("smtp_password");
    	FROM_ADDRESS = properties.getProperty("smtp_fromaddress");
    }

    
    
	public void sendEmails(ArrayList<EmailMessage> emails) throws MessagingException {

		// Suppress all mails
		if (Boolean.parseBoolean(properties.getProperty("suppressAllMails"))) return;
		
        Properties mailProperties = new Properties();
        mailProperties.put("mail.transport.protocol", SMTP_PROTOCOL);
        Session mailSession = Session.getDefaultInstance(mailProperties);
        mailSession.setDebug(true);

        ArrayList<MimeMessage> mimeMessages = new ArrayList<MimeMessage>();
        for (EmailMessage email:emails) {
        	MimeMessage message = new MimeMessage(mailSession);
        	message.setFrom(new InternetAddress(FROM_ADDRESS));
        	message.setSentDate(new Date());
        	message.setSubject(email.getSubject(), "UTF-8");
        	message.setText(email.getMessageBody(), "UTF-8");
        	message.setHeader("Content-Type", "text/plain; charset=UTF-8");
        	// It's possible to send all e-mails to the administrator for debugging purposes
        	if (Boolean.parseBoolean(properties.getProperty("sendAllEmailsToAdmin")))
        		message.addRecipient(Message.RecipientType.TO, new InternetAddress(properties.getProperty("adminMail")));
        	else 
        		message.addRecipient(Message.RecipientType.TO, new InternetAddress(email.getAddressee()));
			mimeMessages.add(message);	
        }
        
        Transport transport = mailSession.getTransport();
        transport.connect(SMTP_HOST, Integer.parseInt(SMTP_HOST_PORT), SMTP_AUTH_USER, SMTP_AUTH_PWD);
        for (MimeMessage m:mimeMessages) {
        	transport.sendMessage(m, m.getRecipients(Message.RecipientType.TO));
        }
        transport.close();
        
    	// Copy message to "Sent" folder as read
    	String folderName = "Inbox.Sent";
    	Store store = mailSession.getStore("imap");
    	store.connect(SMTP_HOST, SMTP_AUTH_USER, SMTP_AUTH_PWD);
    	Folder folder = store.getFolder(folderName);
    	folder.open(Folder.READ_WRITE);
    	for (MimeMessage m:mimeMessages) {
        	m.setFlag(Flag.SEEN, true);
        	folder.appendMessages(new Message[] {m});
    		
    	}
    	store.close();
    	
	}
}